package com.yanqu.road.server.gameplayer.module.player.beauty;

import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.config.skill.SkillUpgradeInfo;
import com.yanqu.road.entity.player.UserBeauty;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.manger.config.SkillMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.List;

public class GlamourAddPatronsFixedValue extends BeautyUpgradeSkillAction {

    public GlamourAddPatronsFixedValue(int beautyId, int position, int skillType, int batch, BeautyModule beautyModule, GamePlayer player) {
        super(beautyId, position, skillType, batch, beautyModule, player);
    }

    @Override
    public void setOldLv() {
        UserBeauty userBeauty = beautyModule.getUserBeauty(beautyId);
        oldLv = userBeauty.getSkill1();
    }

    @Override
    public SkillInfo getSkillInfo() {
        List<Integer> skillList = StringUtils.stringToIntegerList(GameConfig.BEAUTY_PATRONS_SKILL, "\\|");
        return SkillMgr.getSkillInfo(skillList.get(0));
    }

    @Override
    public void calConsumer() {
        SkillInfo skillInfo = getSkillInfo();
        UserBeauty userBeauty = beautyModule.getUserBeauty(beautyId);
        //计算消耗
        long totalNeed = 0;
        for (int i = 0; i < this.batch; i++) {
            //当前等级需要的消耗
            SkillUpgradeInfo consume = SkillMgr.getSkillUpgradeInfo(skillInfo.getUpgradeType(0), this.oldLv + i);
            if(consume == null){
                break;
            }
            if (userBeauty.getSkillExp() < totalNeed + consume.getConsumeCount()){
                //道具不足
                break;
            }else {
                totalNeed += consume.getConsumeCount();
                this.realBatch++;
            }
        }
        this.needConsume = totalNeed;
        if(this.needConsume > 0){
            this.consumer = new Property(GameConfig.GAME_MONEY_BEAUTY_EXP, BigInteger.valueOf(this.needConsume));
        }
        if(this.realBatch <= 0){
            this.ret = GameErrorCode.E_BEAUTY_PATRONS_SKILL_EXP;
        }
    }

    @Override
    public void upgrade() {
        preValidArgument();

        if(this.ret == 0) {
            checkMaxLv();
        }

        if(this.ret == 0){
            calConsumer();
        }
        if(this.ret == 0) {
            UserBeauty userBeauty = beautyModule.getUserBeauty(beautyId);
            userBeauty.setSkillExp(userBeauty.getSkillExp() - this.needConsume);
            userBeauty.setSkill1(userBeauty.getSkill1() + this.realBatch);
        }
    }


}
